package com.mew.slots.core.game.mjk.service;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.mew.common.enm.SlotsWinType;
import com.mew.common.entity.game.WinFree;
import com.mew.common.exception.ResponseCode;
import com.mew.slots.core.common.messge.response.SlotsBonusPlayResponseMessage;
import com.mew.slots.core.common.vo.StrategyGameVO;
import com.mew.slots.core.game.hlsd.bo.HlsdBonusType;
import com.mew.slots.core.game.mjk.bo.MjkBonusType;
import com.mew.slots.core.game.mjk.bo.MjkInRoom;
import com.mew.slots.core.game.mjk.message.MjkBonusHelpEggMessage;
import com.mew.slots.core.game.mjk.message.MjkBonusOpenHatMessage;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;

@Slf4j
public class MjkBonusService {
    public SlotsBonusPlayResponseMessage play(StrategyGameVO player){
        SlotsBonusPlayResponseMessage message=new SlotsBonusPlayResponseMessage();
        WinFree winFree=player.getWinFree();
        if(winFree.getBounsType() != MjkBonusType.parse(player.getBonusType()).getBonusType()  || winFree.getBounsNumber() < 1){
            player.setStat(ResponseCode.BounsCanNotPlay.code());
            return message;
        }

        player.setWinType(SlotsWinType.Special.code());
        player.setInBetNumber(winFree.getBet());

        MjkInRoom in = JSON.parseObject(winFree.getJson(),MjkInRoom.class);
        if(player.getBonusType() == MjkBonusType.HelpEgg.code()){
            message = helpEgg(player,in);
        }else if(player.getBonusType()== MjkBonusType.OpenHat.code()){
            if(player.getOtherMsg().length != 1 || in.getBonusIcons()[player.getOtherMsg()[0]] > 0){
                player.setStat(ResponseCode.BounsParamInvalid.code());
                return message;
            }
            message = openHat(player,in);
        }else{
            player.setStat(ResponseCode.WinFreeValidError.code());
            return message;
        }
        winFree.setJson(JSON.toJSONString(in));
        return message;
    }

    /**
     * 救蛋
     * @param player
     * @return
     */
    private MjkBonusHelpEggMessage helpEgg(StrategyGameVO player,MjkInRoom in){
        MjkBonusHelpEggMessage message=new MjkBonusHelpEggMessage();
        WinFree winFree=player.getWinFree();
        boolean isSuccess = RandomUtil.randomInt(3)!=0;
        message.setIsSuccess(isSuccess?1:0);
        if(isSuccess){
            int rate = RandomUtil.randomInt(3,10);
            message.setRate(rate);
            winFree.setBounsRate(winFree.getBounsRate()+rate);
            in.setSucNum(in.getSucNum()+1);
        }
        winFree.setBounsNumber(winFree.getBounsNumber()-1);
        if(winFree.getBounsNumber()==0){
            message.setIsGameOver(1);
            message.setPayment(winFree.getBounsRate()*winFree.getBet()*player.getLineNumber()*4);
            player.setPaymentNumber(message.getPayment());
            winFree.setBounsType(0);
            winFree.setBounsRate(0);
        }
        player.iconResult(new int[]{
                message.getIsSuccess(),winFree.getBounsRate()
        });
        return message;
    }

    /**
     * 翻帽子
     * @param player
     * @param in
     * @return
     */
    private MjkBonusOpenHatMessage openHat(StrategyGameVO player,MjkInRoom in){
        int open = player.getOtherMsg()[0];
       MjkBonusOpenHatMessage message = new MjkBonusOpenHatMessage();
       int eggType = RandomUtil.randomInt(5);
       if(eggType == 0){
           message.setIsWhiteEgg(1);
           eggType = RandomUtil.randomInt(1,5);
       }
        in.getBonusIcons()[open] = eggType;
       message.setEggType(eggType);
       long num = Arrays.stream(in.getBonusIcons()).filter(i -> i == message.getEggType()).count();
        if(num > 2){
            message.setIsGameOver(1);
            message.setJpNo(eggType);
            player.setWinType(SlotsWinType.Jackpot.code());
            player.getWinFree().setBounsType(0);
            player.getWinFree().setBounsNumber(0);
        }
        player.iconResult(in.getBonusIcons());
       return message;
    }
}
